/**
 * Copyright (c) 2010 icer He <icerhe@gmail.com>
 */
package com.icerhe.trigger.task;

import com.icerhe.trigger.Consts;
import com.icerhe.trigger.R;
import com.icerhe.trigger.RingtonService;
import com.icerhe.trigger.TaskHelper;
import com.icerhe.trigger.TaskWakeLock;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.KeyguardManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

/**
 * AlarmTaskActivity 弹出提醒画面,播放铃声,振动 
 * 
 * @author icer
 *
 */
public class AlarmTaskActivity extends Activity {
	
	private static final String DEFAULT_SNOOZE = "10";
	
	AlarmTask task = null;
	KeyguardManager km =null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//触发的task
		task = (AlarmTask)TaskHelper.getCronTaskById(this, 
				getIntent().getIntExtra(CronTask.INTENT_TASK_ID, 0));
		
		//set window feature
		final Window win = getWindow();
		win.requestFeature(android.view.Window.FEATURE_NO_TITLE);	
		win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                   | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                   | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                   | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
		
		showLayout();
		
		//TODO 设置reciever监听打盹和取消按钮的onClick(为啥要这样= =?)
	}
	
	private void showLayout() {
		LayoutInflater inflater = LayoutInflater.from(this);
		setContentView(inflater.inflate(R.layout.alarm, null));
        /* snooze behavior: pop a snooze confirmation view, kick alarm
        manager. */
		
		TextView title = (TextView)findViewById(R.id.TaskTitle);
		TextView desc = (TextView)findViewById(R.id.TaskDesc);
		title.setText(task.title);
		desc.setText(task.description);
		
		Button snooze = (Button) findViewById(R.id.snooze);
		snooze.requestFocus();
		snooze.setOnClickListener(new Button.OnClickListener() {
		    public void onClick(View v) {
		    	snooze();
		     }
		 });

     //取消按钮,关闭窗口
     findViewById(R.id.dismiss).setOnClickListener(
         new Button.OnClickListener() {
             public void onClick(View v) {
                 dismiss();
             }
      });
	}
	
	//打盹
	private void snooze() {
		//TODO 这里要从alarm task取打盹时间
		int snoozeMinutes = task.getSnoozeDuration();
		final long snoozeTime = System.currentTimeMillis()
        + (1000 * 60 * snoozeMinutes);
		
		Intent intent = new Intent(AlarmTask.SNOOZE_ACTION);
		Bundle bl = new Bundle();
		bl.putInt(AlarmTask.SNOOZE_ID, task.id);
		intent.putExtras(bl);
		PendingIntent sender = PendingIntent.getBroadcast(this, task.id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
		am.set(AlarmManager.RTC_WAKEUP, snoozeTime, sender);
		dismiss();
	}
	
	//停止提醒,停止铃声和振动
	private void dismiss() {	
        stopService(new Intent(this, RingtonService.class));
		//取消Notification
        NotificationManager nm = 
        	(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(AlarmTask.NOTIFICATION_TYPE);
		finish();
	}

	@Override
	protected void onDestroy() {
		Log.d(Consts.TAG, "AlarmTaskActivity Destory ID:"+task.id);
		TaskWakeLock.releaseLock();
		super.onDestroy();
	}

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // Do this on key down to handle a few of the system keys.
        boolean up = event.getAction() == KeyEvent.ACTION_UP;
        switch (event.getKeyCode()) {
            // Volume keys and camera keys dismiss the alarm
            case KeyEvent.KEYCODE_VOLUME_UP:
            case KeyEvent.KEYCODE_VOLUME_DOWN:
            case KeyEvent.KEYCODE_CAMERA:
            case KeyEvent.KEYCODE_FOCUS:
            case KeyEvent.KEYCODE_BACK:
                if (up) {
                	dismiss();
                }
                return true;
            default:
                break;
        }
        return super.dispatchKeyEvent(event);
    }

	@Override
	protected void onPause() {
		Log.d(Consts.TAG, "AlarmTaskActivity onPause Id:"+task.id);
		super.onPause();
	}

	@Override
	protected void onRestart() {
		Log.d(Consts.TAG, "AlarmTaskActivity onRestart Id:"+task.id);
		super.onRestart();
	}

	@Override
	protected void onResume() {
		Log.d(Consts.TAG, "AlarmTaskActivity onResume Id:"+task.id);
		super.onResume();
	}

	@Override
	protected void onStart() {
		Log.d(Consts.TAG, "AlarmTaskActivity onStart Id:"+task.id);
		super.onStart();
	}

	@Override
	protected void onStop() {
		Log.d(Consts.TAG, "AlarmTaskActivity onStop Id:"+task.id);
		super.onStop();
	}

}
